﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyIM2013.Packets;

namespace TinyIM2013
{
    public class PacketSeqCollection
    {
        private static PacketSeqCollection instance;
        private Dictionary<PCmd, byte[]> PacketSequenceDic;
        public PacketSeqCollection()
        {
            PacketSequenceDic = new Dictionary<PCmd, byte[]>();
        }
        public byte[] GetSequenceByCommand(PCmd command)
        {
            foreach (var seq in PacketSequenceDic)
            {
                if (command == seq.Key)
                {
                    return seq.Value;
                }
            }
            return null;
        }
        public void SetSequenceWithCommand(PCmd command, byte[] seq)
        {
            byte[] data = GetSequenceByCommand(command);
            if (data != null)
            {
                if (!data.SequenceEqual(seq))
                {
                    PacketSequenceDic[command] = seq;
                }
            }
            else
            {
                PacketSequenceDic.Add(command, seq);
            }
        }
        public static PacketSeqCollection Instance {
            get 
            {
                if (instance == null)
                {
                    instance = new PacketSeqCollection();
                }
                return instance;
            }
        }
    }
}
